﻿Imports System.IO
Imports AllStateWFA.KofaxXmlSchema
Imports Kofax.ACWFLib

Public Class clsExportData

#Region "Constructor"
    Public Sub New()
        MyBase.New()
    End Sub

    Private _oBatchElement As ACDataElement
    Public Sub New(ByVal oBatchData As ACDataElement)
        Me._oBatchElement = oBatchData
    End Sub

#End Region

#Region "Batch Data extraction"

    Function GetBatchFields(ByVal Fields As ACDataElementCollection) As ImportSession.Batch.BatchField()
        Dim xFields(-1) As ImportSession.Batch.BatchField
        Dim i As Integer = 0

        Dim kField As ACDataElement
        For iCount As Integer = 1 To Fields.Count
            kField = Fields(iCount)
            ReDim Preserve xFields(i)
            xFields(i) = New ImportSession.Batch.BatchField With {.Name = kField.AttributeValue("Name"), .Value = kField.AttributeValue("Value")}
            i += 1
        Next

        Return xFields
    End Function

    Function GetFolders(ByVal Folders As ACDataElementCollection) As KofaxXmlSchema.Folder()
        Dim xFolders(-1) As KofaxXmlSchema.Folder
        Dim xFolder As KofaxXmlSchema.Folder
        Dim i As Integer = 0
        Dim kFolder As ACDataElement

        For iCount As Integer = 1 To Folders.Count
            kFolder = Folders(iCount)

            xFolder = New KofaxXmlSchema.Folder
            With xFolder
                Dim kFields As ACDataElement = kFolder.FindChildElementByName("IndexFields")
                Dim kDocs As ACDataElement = kFolder.FindChildElementByName("Documents")
                .IndexFields = GetFields(kFields.FindChildElementsByName("IndexField"))
                .Documents = GetDocuments(kDocs.FindChildElementsByName("Document"))
            End With

            Try
                Dim oFolderCustomPropertiesElement As Object
                oFolderCustomPropertiesElement = kFolder.FindChildElementByName("FolderCustomProperties")

                Dim oFolderCustomPropertiesCollection As Object
                oFolderCustomPropertiesCollection = oFolderCustomPropertiesElement.FindChildElementsByName("FolderCustomProperty")
            Catch ex As Exception

            End Try

            ReDim Preserve xFolders(i)
            xFolders(i) = xFolder
            i += 1

        Next

        Return xFolders
    End Function

    Function GetFolderStorageStrings(ByVal FolderStrings As ACDataElement) As KofaxXmlSchema.Document.DocumentCustomStorageString()
        Dim kStrings As ACDataElementCollection = FolderStrings.FindChildElementsByName("FolderCustomStorageString")
        Dim xDocStrings(-1) As KofaxXmlSchema.Document.DocumentCustomStorageString
        Dim i As Integer = 0

        Dim kString As ACDataElement
        For iCount As Integer = 1 To kStrings.Count
            kString = kStrings(iCount)

            ReDim Preserve xDocStrings(i)
            xDocStrings(i) = New KofaxXmlSchema.Document.DocumentCustomStorageString _
            With {.Name = kString.AttributeValue("Name"), .Value = kString.AttributeValue("Value")}
            i += 1
        Next

        Return xDocStrings
    End Function

    Function GetBatchHistory(ByVal History As ACDataElementCollection) As KofaxXmlSchema.ImportSession.Batch.BatchHistoryEntry()
        Dim xHistories(-1) As ImportSession.Batch.BatchHistoryEntry
        Dim xHistory As ImportSession.Batch.BatchHistoryEntry
        Dim i As Integer = 0
        Dim kHistory As ACDataElement

        For iCount As Integer = 1 To History.Count
            kHistory = History(iCount)
            xHistory = New ImportSession.Batch.BatchHistoryEntry
            With xHistory
                .ModuleID = kHistory.AttributeValue("ModuleID").ToString
                .ErrorCode = kHistory.AttributeValue("ErrorCode").ToString
                .ErrorText = kHistory.AttributeValue("ErrorText").ToString()
                .State = kHistory.AttributeValue("State").ToString()
                .UserID = kHistory.AttributeValue("UserID").ToString()
                .StationID = kHistory.AttributeValue("StationID").ToString()
                .StartDateTime = kHistory.AttributeValue("StartDateTime").ToString()
                .EndDateTime = kHistory.AttributeValue("EndDateTime").ToString()
                '.ManualKeyStrokes = kHistory.AttributeValue("ManualKeyStrokes").ToString()
                '.OcrKeyStrokes = kHistory.AttributeValue("OcrKeyStrokes").ToString()
                '.IcrKeyStrokes = kHistory.AttributeValue("IcrKeyStrokes").ToString()
                '.OmrKeyStrokes = kHistory.AttributeValue("OmrKeyStrokes").ToString()
                '.BarKeyStrokes = kHistory.AttributeValue("BarKeyStrokes").ToString()
            End With

            ReDim Preserve xHistories(i)
            xHistories(i) = xHistory
            i += 1
        Next

        Return xHistories
    End Function

    Function GetDocuments(ByVal Documents As ACDataElementCollection) As KofaxXmlSchema.Document()
        Dim xDocuments(-1) As KofaxXmlSchema.Document
        Dim xDocument As KofaxXmlSchema.Document
        Dim i As Integer = 0
        Dim kDocument As ACDataElement

        For iCount As Integer = 1 To Documents.Count
            kDocument = Documents(iCount)
            xDocument = New KofaxXmlSchema.Document
            Dim kPage As ACDataElement = kDocument.FindChildElementByName("Pages")
            Dim kIndexFields As ACDataElement = kDocument.FindChildElementByName("IndexFields")

            With xDocument
                .OcrConvertedFileName = kDocument.AttributeValue("OcrConvertedFileName")
                .Confidence = kDocument.AttributeValue("Confidence")
                .FormTypeName = kDocument.AttributeValue("FormTypeName")
                .BatchDocGUID = kDocument.AttributeValue("BatchDocGUID")
                .IndexFields = GetFields(kIndexFields.FindChildElementsByName("IndexField"))
                .Pages = GetPages(kPage.FindChildElementsByName("Page"), .OcrConvertedFileName)
                .Tables = GetTables(kDocument.FindChildElementByName("Tables"))
                .DocumentProperties = GetDocumentProperties(kDocument.FindChildElementByName("DocumentProperties"))
                .DocumentCustomStorageStrings = GetDocumentStorageStrings(kDocument.FindChildElementByName("DocumentCustomStorageStrings"))
            End With

            ReDim Preserve xDocuments(i)
            xDocuments(i) = xDocument
            i += 1
        Next

        Return xDocuments
    End Function

    Function GetTables(ByVal Tables As ACDataElement) As KofaxXmlSchema.Document.Table()
        Dim xTables(-1) As KofaxXmlSchema.Document.Table
        Dim kTables As ACDataElementCollection = Tables.FindChildElementsByName("Table")
        Dim kTable As ACDataElement
        Dim i As Integer = 0

        For iCount As Integer = 1 To kTables.Count
            kTable = kTables(iCount)

            ReDim Preserve xTables(i)
            xTables(i) = New KofaxXmlSchema.Document.Table _
            With {.Name = kTable.AttributeValue("Name").ToString, .TableRows = GetTableRows(kTable.FindChildElementByName("TableRows"))}
            i += 1

        Next

        Return xTables
    End Function

    Function GetTableRows(ByVal TableRows As ACDataElement) As KofaxXmlSchema.Document.TableRow()
        Dim xTableRows(-1) As KofaxXmlSchema.Document.TableRow
        'Dim xTableRow As KofaxXmlSchema.Document.TableRow
        Dim kTableRow As ACDataElement
        Dim kTableRows As ACDataElementCollection = TableRows.FindChildElementsByName("TableRow")
        Dim i As Integer = 0

        For iCount As Integer = 1 To kTableRows.Count
            kTableRow = kTableRows(iCount)
            'xTableRow = New KofaxXmlSchema.Document.TableRow
            Dim kIndexFields As ACDataElement = kTableRow.FindChildElementByName("IndexFields")

            'With xTableRow
            '    .IndexFields = GetFields(kIndexFields.FindChildElementsByName("IndexField"))
            'End With

            ReDim Preserve xTableRows(i)
            'xTableRows(i) = xTableRow
            xTableRows(i) = New KofaxXmlSchema.Document.TableRow With {.IndexFields = GetFields(kIndexFields.FindChildElementsByName("IndexField"))}
            i += 1

        Next

        Return xTableRows
    End Function

    Function GetFields(ByVal Fields As ACDataElementCollection) As KofaxXmlSchema.IndexField()
        Dim xFields(-1) As KofaxXmlSchema.IndexField
        Dim i As Integer = 0
        Dim kField As ACDataElement

        For iCount As Integer = 1 To Fields.Count
            kField = Fields(iCount)
            ReDim Preserve xFields(i)
            xFields(i) = New KofaxXmlSchema.IndexField With {.Name = kField.AttributeValue("Name"), .Value = kField.AttributeValue("Value")}
            i += 1
        Next

        Return xFields
    End Function

    Function GetDocumentProperties(ByVal DocumentProps As ACDataElement) As KofaxXmlSchema.Document.DocumentProperty()
        Dim kProps As ACDataElementCollection = DocumentProps.FindChildElementsByName("DocumentProperty")
        Dim kProp As ACDataElement
        Dim xDocProps(-1) As KofaxXmlSchema.Document.DocumentProperty
        Dim xDocProp As KofaxXmlSchema.Document.DocumentProperty
        Dim i As Integer = 0


        For iCount As Integer = 1 To kProps.Count
            kProp = kProps(iCount)
            xDocProp = New KofaxXmlSchema.Document.DocumentProperty

            With xDocProp
                .BatchID = kProp.AttributeValue("BatchID")
                .Name = kProp.AttributeValue("Name")
                .Value = kProp.AttributeValue("Value")
                .ModuleID = kProp.AttributeValue("ModuleID")
            End With

            ReDim Preserve xDocProps(i)
            xDocProps(i) = xDocProp
            i += 1

        Next

        Return xDocProps
    End Function

    Function GetDocumentStorageStrings(ByVal DocumentStrings As ACDataElement) As KofaxXmlSchema.Document.DocumentCustomStorageString()
        Dim kStrings As ACDataElementCollection = DocumentStrings.FindChildElementsByName("DocumentCustomStorageString")
        Dim xDocStrings(-1) As KofaxXmlSchema.Document.DocumentCustomStorageString
        Dim i As Integer = 0

        Dim kString As ACDataElement
        For iCount As Integer = 1 To kStrings.Count
            kString = kStrings(iCount)

            ReDim Preserve xDocStrings(i)
            xDocStrings(i) = New KofaxXmlSchema.Document.DocumentCustomStorageString _
            With {.Name = kString.AttributeValue("Name"), .Value = kString.AttributeValue("Value")}

            i += 1
        Next

        Return xDocStrings
    End Function

    Function GetPages(ByVal Pages As ACDataElementCollection, ByVal OcrPath As String) As KofaxXmlSchema.Page()
        Dim xPages(-1) As KofaxXmlSchema.Page
        Dim xPage As KofaxXmlSchema.Page
        Dim i As Integer = 0
        Dim sOrcArray(-1) As String

        If OcrPath.Length > 0 Then
            sOrcArray = Split(File.ReadAllText(OcrPath), vbFormFeed)
        End If

        Dim kPage As ACDataElement
        For iCount As Integer = 1 To Pages.Count
            kPage = Pages(iCount)
            xPage = New KofaxXmlSchema.Page
            With xPage
                .ImageID = .ImportFileName = kPage.AttributeValue("ImageID").ToString
                .PageRegConfidence = kPage.AttributeValue("PageRegConfidence").ToString
                .ImageFile = File.ReadAllBytes(kPage.AttributeValue("ImageFile").ToString)
                .PageCustomStorageStrings = GetPageStorageStrings(kPage.FindChildElementByName("PageCustomStorageStrings"))
                .ImportFileName = kPage.AttributeValue("ImageFile").ToString

                'Fill up the ocr text data if available.
                If OcrPath.Length Then
                    .PageText = sOrcArray(i) & vbCrLf & "Page: " & (i + 1).ToString.PadLeft(4, "0") & " ocr data."
                Else
                    .PageText = "Page: " & (i + 1).ToString.PadLeft(4, "0") & " ocr data."
                End If

            End With
            ReDim Preserve xPages(i)
            xPages(i) = xPage
            i += 1
        Next

        Return xPages
    End Function

    Function GetPageStorageStrings(ByVal PageStrings As ACDataElement) As KofaxXmlSchema.Page.PageCustomStorageString()
        Dim kStrings As ACDataElementCollection = PageStrings.FindChildElementsByName("PageCustomStorageString")
        Dim xPageStrings(-1) As KofaxXmlSchema.Page.PageCustomStorageString
        Dim i As Integer = 0

        Dim kString As ACDataElement
        For iCount As Integer = 1 To kStrings.Count
            kString = kStrings(iCount)

            ReDim Preserve xPageStrings(i)
            xPageStrings(i) = New KofaxXmlSchema.Page.PageCustomStorageString _
            With {.Name = kString.AttributeValue("Name"), .Value = kString.AttributeValue("Value")}
            i += 1
        Next

        Return xPageStrings
    End Function

#End Region


End Class
